package com.oocl.ha.merchant.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.w3c.dom.ls.LSInput;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.ha.entity.Dish;
import com.oocl.ha.entity.Order;
import com.oocl.ha.merchant.service.OrderManager;
import com.oocl.ha.merchant.util.ConfigUtil;
import com.oocl.ha.util.JSONUtil;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.media.jfxmedia.Media;

@Controller("moc")
public class OrderController {
	@Autowired
	private OrderManager om;

	@RequestMapping("/merchant/jsp/takeOrder/{oId}")
	@ResponseBody
	public Order takeOrder(@PathVariable int oId) {
		om.updateOrderTaken(oId, 1);
		Order o = om.findOrderById(oId);
		return o;
	}

	@RequestMapping("/merchant/jsp/refuseOrder")
	@ResponseBody
	public Order refuseOrder(int oId) {
		om.updateOrderTaken(oId, -2);
		Order o = om.findOrderById(oId);
		return o;
	}

	@RequestMapping("/merchant/jsp/loadAllOrder")
	public ModelAndView loadAllOrder(@RequestParam int mId) {
		List<Order> orders = om.loadAllOrder(mId);
		return new ModelAndView("orderManager", "orderlist", orders);
	}

	@RequestMapping(value = "/merchant/jsp/loadNotTakenOrder", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String loadNotTakenOrder(HttpSession session) {
		Integer mId = (Integer) session.getAttribute("mId");
		List<Order> notTaken = om.findNotTakenOrders(mId);
		if (notTaken != null) {

			for (Order o : notTaken) {
				System.out.println(o.getDishs());
			}
			List<Order> taken = om.findTakenOrders(mId);
			List<Order> doNotTake = om.findOrderDoNotTake(mId);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("notTaken", notTaken);
			map.put("taken", taken);
			map.put("doNotTake", doNotTake);
			String json = "";
			try {
				json = JSONUtil.objetc2Json(map);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			return json;
		} else {
			return null;
		}
	}

	@RequestMapping(value = "/merchant/jsp/loadHistoryOrder", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String loadHistoryOrder(HttpSession session) {
		Integer mId = (Integer) session.getAttribute("mId");
		List<Order> orders = om.findHistoryOrder(mId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("history", orders);
		String json = "";
		if (orders != null) {
			try {
				json = JSONUtil.objetc2Json(map);
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
			return json;
		} else {
			return "";
		}
	}

	@RequestMapping("/merchant/jsp/loadNotTakenOid")
	public ModelAndView loadNotTakenOid(@RequestParam int mId) {
		List<Integer> oIds = om.findNotTakensOid(mId);
		return new ModelAndView("merchant/jsp/orderManager", "oIds", oIds);
	}

	@RequestMapping("/merchant/jsp/loadOrder")
	public ModelAndView loadOrder(@RequestParam int oId) {
		Order o = om.loadOrder(oId);
		return new ModelAndView("merchant/jsp/MerchantOrderDetail", "order", o);
	}

	@RequestMapping(value = "/merchant/jsp/processComments", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String processComments(int oid) {
		Client client = new Client();
		client.setReadTimeout(1000);
		String webServiceComments = ConfigUtil.getConfigValue("merchant.properties", "webServiceComments");
		String url = webServiceComments + oid;
		WebResource wr = client.resource(url);
		String getStatus = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);

		return getStatus;
	}
}
